// ScriptManager.cpp: implementation of the CScriptManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ansieditor.h"
#include "ScriptManager.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CScriptManager::CScriptManager()
{

}

CScriptManager::~CScriptManager()
{
	ReleaseArrScript();
}

int compare( const void *arg1, const void *arg2 )
{
   CString *ps1 = *(CString**)arg1;
   CString *ps2 = *(CString**)arg2;
   return ps1->CompareNoCase(*ps2);
}

void CScriptManager::OnScriptMenuInitPopup(CMenu *pMenu)
{
	int nCount = pMenu->GetMenuItemCount();
	for(int n = 1; n < nCount; n++)
	{
		pMenu->DeleteMenu(1, MF_BYPOSITION);
	}
	if(gpApp->m_xApplication.m_bScriptIsRunning)
	{
		pMenu->AppendMenu(MF_STRING, IDM_STOP_SCRIPT, gpApp->m_mui.LoadString(IDS_STOP_SCRIPT));
	}
	ReleaseArrScript();
	WIN32_FIND_DATA fd;
	CString &sAppPath = gpApp->GetAppPath();
	HANDLE hFind = FindFirstFile(sAppPath + "\\Script\\*.*", &fd);
	if(hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if(fd.cFileName[0] == '.')
			{
				continue;
			}
			CString sPostFix = GetFilePostFix(fd.cFileName);// sFileName.Mid(nPos);
			if(sPostFix.CompareNoCase("vbs") == 0 || 
			   sPostFix.CompareNoCase("js") == 0)
			{
				m_arrScript.Add(new CString(fd.cFileName));
			}
		} while(FindNextFile(hFind, &fd));
	}
	FindClose(hFind);
	
	nCount = m_arrScript.GetSize();
	if(nCount == 0)
	{
		return;
	}
	qsort((LPVOID)m_arrScript.GetData(), nCount, sizeof(CString*), compare);
	pMenu->AppendMenu(MF_SEPARATOR, 0);
	CString sMenu;
	for(n = 0; n < nCount; n++)
	{
		sMenu.Format("&%d  %s", n + 1, *m_arrScript.GetAt(n));
		pMenu->AppendMenu(MF_STRING, IDM_SCRIPT_BASE + n, sMenu);
	}
}

void CScriptManager::ReleaseArrScript()
{
	int nCount = m_arrScript.GetSize();
	for(int n = 0; n < nCount; n++)
	{
		delete m_arrScript.GetAt(n);
	}
	m_arrScript.RemoveAll();
}

void CScriptManager::OnScriptMenu(UINT nID)
{
	int nIndex = nID - IDM_SCRIPT_BASE;
	CString &sScriptFile = *m_arrScript.GetAt(nIndex);
	enumScriptType scriptType = GetScriptType(sScriptFile);
	CString sScriptPath;
	sScriptPath.Format("%s\\script\\%s", gpApp->GetAppPath(), sScriptFile);
	CString sScriptText;
	if(!ReadTextFile(sScriptPath, sScriptText))
	{
		ShowError(gpApp->m_mui.LoadString(IDS_OPENSCRIPT_FAILED));
	}
	ShowError(sScriptText);
	gpApp->m_xApplication.RunScript(sScriptText, scriptType);
}

CString CScriptManager::GetFilePostFix(LPCTSTR szFileName)
{
	int nLen = lstrlen(szFileName);
	for(int n = nLen - 1; n >= 0; n--)
	{
		if(szFileName[n] == '.')
		{
			CString sResult = szFileName + n + 1;
			return sResult;
		}
	}
	return "";
}

enumScriptType CScriptManager::GetScriptType(LPCTSTR szFileName)
{
	CString sPostFix = GetFilePostFix(szFileName);
	if(sPostFix.CompareNoCase("vbs") == 0)
	{
		return ScriptType_VBScript;
	}
	else if(sPostFix.CompareNoCase("js") == 0)
	{
		return ScriptType_JScript;
	}
	return ScriptType_Unknown;
}

void CScriptManager::StopScript()
{
	gpApp->m_xApplication.StopEngine();
}

void CScriptManager::RunStartupScript()
{
	CString sFilePath = gpApp->GetAppPath() + "\\AutoRun.vbs";
	enumScriptType scriptType = ScriptType_VBScript;
	if(!FileExists(sFilePath))
	{
		sFilePath = gpApp->GetAppPath() + "\\AutoRun.js";
		scriptType = ScriptType_JScript;
	}
	if(!FileExists(sFilePath))
	{
		return;
	}
	CString sScriptText;
	if(!ReadTextFile(sFilePath, sScriptText))
	{
		return;
	}
	gpApp->m_xApplication.RunScript(sScriptText, scriptType);
}